package 集合.练习题;

import java.util.ArrayList;
import java.util.Comparator;

public class lesson02 {
    public static void main(String[] args) {
        ArrayList<Object> book01s = new ArrayList<>();
        book01s.add(new Book01("水浒传",15));
        book01s.add(new Book01("三国演义",10));
        book01s.add(new Book01("西游记",30));
        book01s.add(new Book01("红楼梦",9));

        sort01(book01s, new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                Book01 book1 = (Book01)o1;
                Book01 book2 = (Book01)o2;
                double price = book1.getPrice() - book2.getPrice();
                if(price > 0){
                    return 1;
                }else if(price < 0){
                    return -1;
                }else {
                    return 0;
                }

            }
        });
        System.out.println(book01s);

    }

    public static void sort01(ArrayList<Object> arrayList, Comparator<Object> comparator){
        Object temp = null;
        for(int i = arrayList.size() - 1; i>=0; i--){
            for(int j = 0; j<i; j++){
                if(comparator.compare(arrayList.get(j),arrayList.get(j + 1)) > 0){
                    Book01 book01 = (Book01) arrayList.get(j);
                    Book01 book02 = (Book01)arrayList.get(j + 1);
                    arrayList.set(j, book02);
                    arrayList.set(j + 1, book01);
                }

            }
        }
    }
}

class Book01{
    private String name;
    private int price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "\nBook01{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }

    public Book01(String name, int price) {
        this.name = name;
        this.price = price;
    }
}